<template>
  <div class="header">
    <el-card>
      <div>
        <el-button type="primary" @click="goupload">导入</el-button>
        <el-button type="primary" @click="addClick">+ 新增员工</el-button>
      </div>
    </el-card>
    <el-card style="margin-top: 20px">
      <el-table :data="list" style="width: 100%">
        <el-table-column label="序号" width="50px">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <!-- <el-table-column
          label="聘用形式"
          prop="formOfEmployment"
          width="80"
          :formatter="formatter"
        /> -->
        <el-table-column label="聘用形式" prop="formOfEmployment" width="80" />
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column
          label="入职日期"
          prop="timeOfEntry"
          :formatter="formattertimeOfEntry"
        />
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="look(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text">角色</el-button>
              <el-button type="text" @click="delClick(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div class="page">
      <el-pagination
        :current-page="page.page"
        :page-sizes="[1, 2, 3, 10, 200, 300, 400]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 新增员工弹框 -->
    <Add ref="add" @getData="getData" />
    <!-- 导入弹框 -->
    <!-- <Excel ref="upload" @getData="getData" /> -->
  </div>
</template>
<script>
// import Excel from './component/excel.vue'
import { sysUser, sysUserDel } from '@/api/employees'
import Add from './component/add.vue'
import employeesData from '@/api/constant/employees'
import dayjs from 'dayjs'
export default {
  name: '',
  components: { Add },
  data() {
    return {
      isShow: false,
      list: [],
      page: {
        page: 1,
        size: 10,
        total: 100
      }
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    // 每页显示多少条
    handleSizeChange(size) {
      this.page.size = size
      this.getData()
      console.log(size)
    },
    // 当前页码
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
      console.log(page)
    },
    async getData() {
      const res = await sysUser(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
      console.log(res)
      res.data.rows.forEach((item) => {
        item.formOfEmployment = this.formatter(
          null,
          null,
          item.formOfEmployment
        )
      })
    },
    // 格式化聘用形式
    formatter(row, column, cellValue, index) {
      const temp = employeesData.hireType.find((item) => {
        return item.id === +cellValue
      })
      return temp ? temp.value : '-'
    },
    // 格式化入职时间
    formattertimeOfEntry(row, column, cellValue, index) {
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    // 点击新增员工显示弹框
    addClick() {
      this.$refs.add.isShow = true
    },
    // 删除员工
    delClick(row) {
      this.$confirm('确定要删除该员工吗？', '提示')
        .then(async() => {
          await sysUserDel(row.id)
          this.$message.success('删除成功')
          if (this.page.page > 1 && this.list.length === 1) {
            this.page.page--
          }
          this.getData()
        })
        .catch(() => {})
    },
    // 导入按钮
    goupload() {
      this.$router.push('/employees/upload?name=' + encodeURIComponent('员工'))
      // this.$refs.upload.dialogVisible = true
    },
    // 点击查看
    look(row) {
      this.$router.push('/employees/detail/' + row.id)
    }
  }
}
</script>
<style lang="scss" scoped>
.header {
  padding: 15px;
  text-align: right;
}
.page {
  margin-top: 20px;
  text-align: right;
}
</style>
